package com.tomehay.termprojectmanage_server.controller;

import com.tomehay.termprojectmanage_server.pojo.Model.Code;
import com.tomehay.termprojectmanage_server.pojo.Model.Document;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.DocumentService;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Document)控制层
 *
 * @author makejava
 * @since 2023-06-24 15:54:10
 */
@RestController
@RequestMapping("/api/document")
public class DocumentController {
    /**
     * 服务对象
     */
    @Resource
    private DocumentService documentService;

    /**
     * 通过主键查询单条数据
     *
     * @param document 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public ResultVO<Document> selectOne(Document document) {
        Document result = documentService.selectById(document.getId());
        if(result != null){
            return ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }
    
    /**
     * 新增一条数据
     *
     * @param document 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ResultVO<Document> insert(@RequestBody Document document) {
        int result = documentService.insert(document);
        if (result > 0) {
            return ResultVO.createSuccessResponse(document);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 修改一条数据
     *
     * @param document 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResultVO<Document> update(@RequestBody Document document) {
        Document result = documentService.update(document);
        if (result != null) {
            return ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 删除一条数据
     *
     * @param document 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ResultVO<Document> delete(Document document) {
        int result = documentService.deleteById(document.getId());
        if (result > 0) {
            return ResultVO.createSuccessResponse(null);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public ResultVO<List<Document>> selectAll() {
        List<Document> documents = documentService.selectAll();
        if (documents != null) {
            return ResultVO.createSuccessResponse(documents);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public ResultVO<List<Document>> selectPage(Integer start, Integer limit) {
        List<Document> documents = documentService.selectPage(start, limit);
        if (documents != null) {
            return ResultVO.createSuccessResponse(documents);
        }
        return ResultVO.createErrorResponse();
    }

    @GetMapping("/listByProjectId")
    public ResultVO<List<Document>> listByProjectId(@RequestParam(value = "projectId") Integer projectId) {
        List<Document> codes = documentService.listByProjectId(projectId);
        if (codes != null) {
            return ResultVO.createSuccessResponse(codes);
        }
        return ResultVO.createErrorResponse();
    }

    @PostMapping("/uploadCode")
    public void uploadCode(@RequestParam("no") String no, @RequestParam("description") String description, @RequestParam("projectId") Integer pid, @RequestParam("gmno") Integer gmno, @RequestParam("gno") Integer gno,@RequestPart("file") MultipartFile file) {
        documentService.uploadCode(no, description,pid, gmno, gno, file);
    }


    @GetMapping("/download")
    @ResponseBody
    public ResponseEntity<InputStreamResource> download(@RequestParam(value = "id") Integer id) {
        return documentService.download(id);
    }
    
}

